<template>
	<view class="wrap safe-area flex column align-center">
		<view class="wrap-white-bg">
			<block v-for="(item, index) in list" :key="index">
				<rowInput
					v-if="
						item.type === 'input' &&
						item.direct === 'row' &&
						item.isShow
					"
					:item="item"
					v-model="dataInfo[item.key]"
				/>
	
				<rowGroup
					v-else-if="item.type === 'group' && item.direct === 'row'"
					:item="item"
					v-model="dataInfo[item.key]"
				/>
			</block>
		</view>
	
		<view class="sure-btn theme-gradient-btn f-34" @click="onSave">保存</view>
	</view>
</template>

<script>
	import { getUserInfo } from "@/API/mine.js";
	import {FillDuty} from "@/API/invoice.js";
	import rowGroup from "@/components/form/rowGroup.vue";
	import rowInput from "@/components/form/rowInput.vue";
	export default {
		components: {
			rowInput,
			rowGroup,
		},
		data() {
			return {
				dataInfo: {
					invoice_type: "1", //发票类型:1=普票,2=专票
					headup_type: "1", //抬头类型:1=个人,2=单位
					headup: "", //抬头
					duty_paragraph: "", //税号
				},
			}
		},
		computed: {
			list() {
				return [
					{
						title: "发票类型",
						require: true,
						type: "group",
						direct: "row",
						key: "invoice_type",
						tips: "请选择发票类型",
						list: [
							{
								id: "1",
								title: "普票",
							},
							{
								id: "2",
								title: "专票",
							},
						],
					},
					{
						title: "抬头类型",
						require: true,
						type: "group",
						direct: "row",
						key: "headup_type",
						tips: "请选择抬头类型",
						list: [
							{
								id: "1",
								title: "个人",
							},
							{
								id: "2",
								title: "单位",
							},
						],
					},
					{
						title: "发票抬头",
						require: true,
						type: "input",
						key: "headup",
						place: "请输入发票抬头",
						mode: "text",
						maxlength: 20,
						direct: "row",
						tips: "请输入发票抬头",
						isShow: true,
					},
					{
						title: "税号",
						require: true,
						type: "input",
						key: "duty_paragraph",
						place: "请输入税号",
						mode: "text",
						maxlength: 20,
						direct: "row",
						tips: "请输入税号",
						isShow: this.dataInfo.headup_type === "2",
					},
				]
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			/**
			 * 保存
			 */
			onSave() {
				if (this.$filters.judgeFormData(this.list, this.dataInfo)) {
					this.$throttle(() => {
						FillDuty(this.dataInfo).then(res => {
							this.$st.success("保存成功", () => {
								this.getData();
							})
						})
					});
				}
			},
			/**
			 * 网络请求
			 */
			getData() {
				getUserInfo().then(res => {
					if (res.data.userinfo.duty_info !== null) {
						Object.keys(this.dataInfo).forEach(key => {
							this.dataInfo[key] = res.data.userinfo.duty_info[key];
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/css/publish.css";
</style>